/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import entity.Employee;
import entity.PlainTextMessage;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.event.ActionEvent;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import message.AnnounceMeManager;
import session.stateless.AnnouncementSessionLocal;
import session.stateless.EmployeeBeanLocal;

/**
 *
 * @author Vincent
 */
@ManagedBean(name="announcementManagerBean")
@ViewScoped
public class AnnouncementManagerBean implements Serializable{
    
    @EJB 
    AnnouncementSessionLocal announcementSessionLocal; 
    @EJB
    EmployeeBeanLocal employeeSessionBeanLocal;
    
    private AnnounceMeManager announcementManager;
    private List<PlainTextMessage> announcement;
    private String statusMessage;
    private String announcementText;
    private String title;
    private String sender;
    private Employee employee;
    private PlainTextMessage selectedAnnouncement;
    
    public List<PlainTextMessage> getMessages() {
        return announcement;
    }

    public List<PlainTextMessage> getAnnoucement() {
        return announcement;
    }

    public void setAnnoucement(List<PlainTextMessage> annoucement) {
        this.announcement = annoucement;
    }

    public String getAnnoucementText() {
        return announcementText;
    }

    public void setAnnoucementText(String annoucementText) {
        this.announcementText = annoucementText;
    }
    

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public PlainTextMessage getSelectedAnnouncement() {
        return selectedAnnouncement;
    }

    public void setSelectedAnnouncement(PlainTextMessage selectedAnnouncement) {
        this.selectedAnnouncement = selectedAnnouncement;
    }

    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<PlainTextMessage> getAnnouncement() {
        return announcement;
    }

    public void setAnnouncement(List<PlainTextMessage> announcement) {
        this.announcement = announcement;
    }

    public String getAnnouncementText() {
        return announcementText;
    }

    public void setAnnouncementText(String announcementText) {
        this.announcementText = announcementText;
    }
    
    
        /** Creates a new instance of MessageMeManagerBean */
    public AnnouncementManagerBean() {
    }
    
    public void initView(PhaseEvent event){
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        employee = controller.getEmployee();
        announcementManager = new AnnounceMeManager(announcementSessionLocal); 
        announcement = announcementManager.getAllMessages();
    }
    
    public void refresh(ActionEvent event){
         announcementManager = new AnnounceMeManager(announcementSessionLocal); 
         announcement = announcementManager.getAllMessages();
         statusMessage = "Refresh Successfully."; 

         FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Refresh Result: " + statusMessage, "")); 
    }
    
    public void send(ActionEvent event){
         sender = employee.getUserName();
        
         announcementManager = new AnnounceMeManager(announcementSessionLocal); 
         announcementManager.announce(this.announcementText, sender, title);
         
         sender="";
         title="";
         announcementText="";
         statusMessage = "Send Successfully."; 

         FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Refresh Result: " + statusMessage, "")); 
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<Employee> list = employeeSessionBeanLocal.getEmployees();
        
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getUserName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getUserName());
        }
        return results;  
    }
    

}
